﻿using MicroMVVM.Services.Interfaces;
using System.Reactive.Subjects;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;

namespace MicroMVVM.Services.Implementation.Any
{
    /// <summary>
    /// This interface represents the service to check the current tablet layout.
    /// </summary>
    [ExportService(ServiceType.Any, typeof(IViewLayoutService), PartCreationPolicy.Singleton)]
    public class ViewLayoutService : IViewLayoutService
    {
        private ApplicationViewState _currentApplicationViewState;

        public Subject<ApplicationViewState> OnViewStateChanged { get; set; }

        public ApplicationViewState CurrentViewState { get { return _currentApplicationViewState; } }

        public ViewLayoutService()
        {
            OnViewStateChanged = new Subject<ApplicationViewState>();
            Window.Current.SizeChanged += CurrentWindowSizeChanged;
            _currentApplicationViewState = ApplicationView.Value;
        }

        void CurrentWindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            if (ApplicationView.Value == _currentApplicationViewState) return;

            _currentApplicationViewState = ApplicationView.Value;
            OnViewStateChanged.OnNext(_currentApplicationViewState);
        }
    }
}